import {Body, Controller, HttpStatus, Post, Res} from '@nestjs/common';
import {Response} from 'express';
import {LogListService} from '../../service/log.list.service';
import {CommonResponseDto} from '../../dto/common.response.dto';
import {ResCode} from '../../common/constants';
import {getLogStr} from "../../common/utils";
import {Logger} from '../../utils/log4js';
import {uuid} from "../../common/utils";

const exception = (e) => {
    console.log(e);
    try {
        Logger.error(getLogStr(e))
    } catch(e){}
    return new CommonResponseDto(ResCode.ERROR, '服务异常', null);
}
@Controller('log')
export class LogListController {
    constructor(private readonly logListService: LogListService) {}

    @Post('/list')
    async getList(@Body() params: {startTime: number, endTime: number, pageIndex: number, pageSize: number}, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        const ts = new Date().getTime()
        params.startTime = params.startTime || ts
        params.endTime = params.endTime || ts
        params.pageIndex = params.pageIndex || 1
        params.pageSize = params.pageSize || 10

        try {
            const data = await this.logListService.getLogListByTimeRange({startTime: params.startTime, endTime: params.endTime, pageIndex: params.pageIndex, pageSize: params.pageSize})
            if (data && data.length === 2) {
                return new CommonResponseDto(ResCode.OK, 'success', {
                    list: data[0],
                    total: data[1]
                })
            } else {
                return new CommonResponseDto(ResCode.OK, 'success', null)
            }

        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }

    @Post('/save')
    async saveUser(@Body() params: {type: string, deviceSn: string, jobId: string, waylineId: string, userId: string, userName: string, userIp: string }, @Res({ passthrough: true }) res: Response) {
        res.status(HttpStatus.OK);
        const id = uuid(32)
        const time = new Date().getTime()
        params.type = params.type || '';
        params.deviceSn = params.deviceSn || '';
        params.jobId = params.jobId || '';
        params.waylineId = params.waylineId || '';
        params.userId = params.userId || '';
        params.userName = params.userName || '';
        params.userIp = params.userIp || '';
        if (!params.type || !params.userId || !params.userName) {
            return new CommonResponseDto(ResCode.ERROR, 'type userId userName 必填', null)
        }
        try {
            let data = await this.logListService.createOne({
                id,
                type: params.type,
                deviceSn: params.deviceSn,
                jobId: params.jobId,
                waylineId: params.waylineId,
                userId: params.userId,
                userName: params.userName,
                userIp: params.userIp,
                time,
            })
            return new CommonResponseDto(ResCode.OK, 'success', data)
        } catch(e){
            console.log(e)
            return new CommonResponseDto(ResCode.ERROR, '服务异常', null)
        }
    }
}
